import Watcher from "./watcher.js"
import Dep from "./dep.js"

export default {
  model (el, vm, key, eventName) {
    const w = new Watcher(vm, key, (newV) => {
      this.setModelValue(el, newV)
    })
    Dep.target = w
    const oldValue = this.getValue(vm, key)
    this.setModelValue(el, oldValue)
    el.addEventListener('input', (e) => {
      const val = e.target.value
      this.setValue(vm, key, val)
    })
  },
  on (el, vm, key, eventName) {
    el.addEventListener(eventName, () => {
      vm.$methods[key].call(vm)
    })
  },
  text (el, vm, key, eventName) {
    const w = new Watcher(vm, key, (newV) => {
      el.textContent = newV
    })
    Dep.target = w
    el.textContent = this.getValue(vm, key)
  },
  setModelValue(el,val) {
    el.value = val
  },
  getValue (vm, key) {
    return vm.$data[key]
  },
  setValue (vm, key, newVal) {
    return vm.$data[key] = newVal
  }
}